﻿/*
   进公司批次,每天通过server导数据时判断有无新加的员工，如果有，
 * 则根据GlobalConfig的BatchDays来算出是否需要增加一个批次，
 * 或者可以通过管理员登陆提示管理员由一个新的批次要产生
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/*
    2012-2-17 David检查正常
 */
namespace BenefitHelper.Sys
{
    /// <summary>
    /// 进公司批次
    /// </summary>
    public class Batch
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 添加时间
        /// </summary>
        public DateTime AddDate { get; set; }
        /// <summary>
        /// 批次别名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 得到最新的一个批次
        /// </summary>
        /// <returns></returns>
        public int GetNewBatch()
        {
            using (DB.DBManager db = new DB.DBManager())
            {
                int id = 0;
                var query = from t in db.Batches orderby t.AddDate descending select t;
                if (query.Count() > 0)
                {
                    if (DateTime.Now.AddDays(-Config.DefaultValue.BatchBetween) > query.First().AddDate)
                    {
                        Batch bh = new Batch();
                        bh.AddDate = DateTime.Now;
                        bh.Name = "批次";
                        db.Batches.Add(bh);
                        db.SaveChanges();
                        bh.Name = "第" + bh.Id + "批次";
                        db.SaveChanges();
                        id = bh.Id;
                    }
                    else
                    {
                        id = query.First().Id;
                    }
                }
                else
                {
                    Batch bh = new Batch();
                    bh.AddDate = DateTime.Now;
                    bh.Name = "第1批次";
                    db.Batches.Add(bh);
                    db.SaveChanges();
                    id = bh.Id;
                }
                db.Dispose();
                return id;
            }
        }
        /// <summary>
        /// 返回所有批次
        /// </summary>
        /// <returns></returns>
        internal List<Batch> GetList(DB.DBManager db)
        {
            var query = db.Batches.ToList();
            return query;
        }
    }
}